package dao.Impl;

import dao.BaseDao;
import dao.financingproduct;
import entity.Financingproduct;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class financingproductImpl extends BaseDao implements financingproduct {
    @Override
    public List<Financingproduct> select(int id,String risk) {
        StringBuilder sql=new StringBuilder("select * from financingproduct");
        if (id!=0 || risk!=null){
            sql.append(" where ");
        }

        if (id!=0){
            sql.append(" id like ? ");
        }
        if (risk!=null){
            if (id!=0){
                sql.append(" and");
            }
            sql.append(" risk=? ");
        }
        Object[] shuju={id,risk};
        List<Financingproduct> list=new ArrayList<>();
        Financingproduct financingproduct=null;
        ResultSet rs =executeQuery(String.valueOf(sql),shuju);
        try {
            while (rs.next()){
                financingproduct=new Financingproduct();
                financingproduct.setId(rs.getInt("id"));
                financingproduct.setRisk(rs.getString("risk"));
                financingproduct.setIncome(rs.getString("income"));
                financingproduct.setSaleStarting(rs.getDate("saleStarting"));
                financingproduct.setSaleEnd(rs.getDate("saleEnd"));
                financingproduct.setEnd(rs.getDate("end"));
                list.add(financingproduct);

            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(connection,pstmt,rs);
        }
        return list;
    }

    @Override
    public int add(Financingproduct f) {
        String sql="insert into financingproduct(id,risk,income,saleStarting,saleEnd,end) values(?,?,?,?,?,?)";
        Object params[]={f.getId(),f.getRisk(),f.getIncome(),f.getSaleStarting(),f.getSaleEnd(),f.getEnd()};
        int hanghsu=executeUpdate(sql,params);
        return hanghsu;
    }
}
